<?
##########################################################################
# 			     DOCUMENTATION
##########################################################################
# Module to upload new books in the project. It can handle files given
# directly but also create new files from data given through a textarea and
# some input tags.
#
# © LiberLab group, 2011
##########################################################################

require_once("dataStructure/user.php");
require_once("dynamic/base/sendData.php");

session_start();

class Upload extends SendData{
	public function __construct() {
		parent::__construct("upload", "upload");
	}
	
	public function main(){
		$errorPage = "error.html";
		$okPage = "upload2.html";
		
		if (!$this->checkAccess()){
			return $this->backOff();
		}

		# basic error, just in case (should never been used, though)
		$this->tagHash["content"] = "<h1 lang=\"en\" xml:lang=\"en\">Error</h1>\n<p lang=\"en\" xml:lang=\"en\">ERROR, GENERIC.</p>";	

		$pageToLoad = "";
		$maxDimText = 30000;

		$bookName = getParam("book_name", NULL);
		# load some parameters
		$bookDescription = getParam("book_description", " "); # empty description from default
		$bookCategory = strtolower(getParam("book_category", NULL));
		$isbn = strtolower(getParam("book_isbn", NULL));
		$text = getParam("text", NULL);
		
		if ($isbn and (strlen($isbn) != 10 or !ctype_digit($isbn))){
			$this->tagHash["content"] = $this->messages["wrongISBN"];
			$pageToLoad = $errorPage;
		}
		# no text, directly a file given
		if($bookName and $bookCategory and !$pageToLoad and !$text){
			$filename = "";
			if (isset($_FILES["htmlbook"]) and !$_FILES["htmlbook"]["error"]){
				$sizeText = $_FILES["htmlbook"]["size"]; 
				$typeText = $_FILES["htmlbook"]["type"]; /* mime-type */
				$filename = $_FILES["htmlbook"]["name"]; /* name on the client */
				$tmpUText = $_FILES["htmlbook"]["tmp_name"]; /* name on the server */
				$text = file_get_contents($tmpUText);
				unlink($tmpUText);
				if ($typeText != "text/html" and $typeText != "text/plain"){
					$parts = explode(".", $filename);
					$wrongExtension = str_replace("__extension__", $parts[count($parts)-1], $wrongExtension);
					$this->tagHash["content"] = $this->messages["wrongExtension"];
					$pageToLoad = $errorPage;		
				}
				if ($sizeText > $maxDimText) {
					$this->tagHash["content"] = $this->messages["tooBigFile"];
					$pageToLoad = $errorPage;
				}
			}else{
				$this->tagHash["content"] = $this->messages["missingText"];
				$pageToLoad = $errorPage;	
			}
		}else if (!$pageToLoad){
			if (!$bookName){
				$pageToLoad = $errorPage;	
				$this->tagHash["content"] = $this->messages["missingBookName"];
			}else if (!$bookCategory){
				$pageToLoad = $errorPage;	
				$this->tagHash["content"] = $this->messages["noCategory"];	
			}else if (!$text){
				$pageToLoad = $errorPage;	
				$this->tagHash["content"] = $this->messages["missingText"];
			}
		}
		# deal with the cover
		$filenameCover = "";
		
		if (isset($_FILES["book_cover"]) and !$_FILES["book_cover"]["error"]){
			$sizeCover = $_FILES["book_cover"]["size"]; 
			$typeCover = $_FILES["book_cover"]["type"]; /* mime-type */
			$filenameCover = $_FILES["book_cover"]["name"]; /* name on the client */
			$tmpCover = $_FILES["book_cover"]["tmp_name"]; /* name on the server */

			# do the move otherwise when we send the data to upload2.php we will loose the temp image!
			@move_uploaded_file($tmpCover, $tmpCover . "3");
			$tmpCover = $tmpCover . "3"; 
			$parts = explode(".", $filenameCover);
			$coverExtension = $parts[count($parts)-1];
		}else if(!$pageToLoad){
			$this->tagHash["content"] = $this->messages["noCoverGiven"];
			$pageToLoad = $errorPage;	
		}
		if (!$pageToLoad and !($coverExtension == "jpg" or $coverExtension == "png" or $coverExtension == "svg")){
			$wrongCoverExtension = str_replace("__extension__", $coverExtension, $this->messages["wrongCoverExtension"]);
			$this->tagHash["content"] = $wrongCoverExtension;
			$pageToLoad = $errorPage;
		}
		
		$userClass = new User();

		if ($bookName and !$pageToLoad){
			$userIDs = $userClass->getUserId($this->getSession("nickname"), true);
			$this->setSessionArray(array("title" => $bookName, "description" => $bookDescription, "category" => $bookCategory, "content" => $text,
										 "userId" => $userIDs->id, "isbn" => $isbn, "tmpCover" => $tmpCover, "coverExtension" => $coverExtension));
			$pageToLoad = $okPage;
		}
		if ($pageToLoad == $errorPage){
			$this->tagHash["content"] = $this->tagHash["content"] . "\n" . $this->messages["comeBack"];
		}
		if (!$pageToLoad){
			$pageToLoad = $errorPage;
		}
		return $this->render($pageToLoad);
	}

	public function __destruct() {
		parent::__destruct();
	}
};

$upload = new Upload();
echo $upload->main();

?>
